define(function(require, exports, module) {

  var Backbone = require('backbone');
  var $ = require('jquery');
  var _ = require('underscore');
  var jsonview = require('jsonview');
  var tmpl = require('text!../tmpl.html');

  function main() {
    var $app = $('<div id="app">');
    $('#main').html($app);

    $app.append(tmpl);

    $("#header_wrap").click(function(e) {
      console.log(e);
    })

    //http://api.map.baidu.com/api?v=2.0&ak=qwrkG91zKLD62URDrgUolTVh

    window.BMap_loadScriptTime = (new Date).getTime();
    require(['json!./count.json', 'http://api.map.baidu.com/getscript?v=2.0&ak=qwrkG91zKLD62URDrgUolTVh&services=&t=20190123111209'], function(data) {

      // 用经纬度设置地图中心点
      function theLocation(opoint) {
        var sContent = "<h4>" + opoint.description + " " + opoint.wtp_id + "</h4>"
          + "<p><b>Station总数量:</b>" + opoint.count + "</p>"
          + "<p><b>在线数目:</b>" + opoint.online + "</p>"
          + "<p><b>离线数目:</b>" + opoint.offline + "</p>";
        var point = new BMap.Point(opoint.x, opoint.y);
        var myIcon = new BMap.Icon("/assets/img/mark_red.png", new BMap.Size(32,32));
        var marker = new BMap.Marker(point, {icon:myIcon});
        marker.enableDragging();
        var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
        // map.centerAndZoom(point, 15);
        map.addOverlay(marker);
        marker.addEventListener("mouseover", function(e) {
          this.openInfoWindow(infoWindow);
          var pt = e.point;
          geoc.getLocation(pt, function(rs){
            var addComp = rs.addressComponents;
            $("#main_content_wrap").html(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
          });   
        });

        marker.addEventListener("mousedown", function(e) {
          var pt = e.point;
          geoc.getLocation(pt, function(rs){
            var addComp = rs.addressComponents;
            $("#main_content_wrap").html(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
          });   
        });
      }

      $("#footer_wrap").html("<pre>" + jsonview(data) + "</pre>");

      // 百度地图API功能
      var map = new BMap.Map("allmap");
      map.centerAndZoom(new BMap.Point(data[0].x, data[0].y),11);
      map.enableScrollWheelZoom(true);

      var geoc = new BMap.Geocoder();

      var menu = new BMap.ContextMenu();
      var txtMenuItem = [{
        text:'放在这里',
        callback:function() {
            
        }
      }];

      for (var i = 0; i < txtMenuItem.length; i++) {
        menu.addItem(new BMap.MenuItem(txtMenuItem[i].text, txtMenuItem[i].callback, 100));
      }

      map.addContextMenu(menu);

      for (var i = 0; i < data.length; i++) {
        theLocation(data[i]);
      }
    })
    
  }

  return main;
})
